package com.mentaleye.biz.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.PermissionsBean;
import com.mentaleye.biz.PermissionsBiz;
import com.mentaleye.dal.model.Permissions;
import com.mentaleye.dal.model.PermissionsExample;
import com.mentaleye.service.PermissionsService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;


@Service
public class PermissionsBizImpl implements PermissionsBiz{

	private static Logger logger = Logger.getLogger(PermissionsBiz.class);

	@Autowired private PermissionsService permissionsService;
	@Override
	public CommonResult editPermissions(PermissionsBean permissionsBean) {
		CommonResult commonResult=new CommonResult();
		boolean success=true;
		boolean add;
		try {
			if(add=permissionsBean.getPermissionsId()==null){
			success=permissionsService.editPermissions(permissionsBean, add);
			if(success){
				List<PermissionsBean> beans= permissionsService.queryPermissions(permissionsBean.getParentpermissionsId(), permissionsBean.getPermissionsName());
				for (PermissionsBean bean : beans) {
					bean.setPermissionsUrl(bean.getParentpermissionsId()+";"+bean.getPermissionsId()+";");
					permissionsService.editPermissions(bean, false);
				}
			}
			}else{
				success=permissionsService.editPermissions(permissionsBean, add);
				if(success){
					List<PermissionsBean> beans= permissionsService.queryPermissions(permissionsBean.getParentpermissionsId(), permissionsBean.getPermissionsName());
					for (PermissionsBean bean : beans) {
						bean.setPermissionsUrl(bean.getParentpermissionsId()+";"+bean.getPermissionsId()+";");
						permissionsService.editPermissions(bean, false);
					}
				}
			}
			commonResult.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("编辑权限异常",e);
		}
		return commonResult;
	}

	@Override
	public CommonResult deletePermissions(List<Integer> permissionsId) {
		CommonResult result=new CommonResult();
		boolean success=false;

		try {
			List<PermissionsBean> permissionsBeans=permissionsService.queryPermissions(permissionsId);
			for (PermissionsBean permissionsBean : permissionsBeans) {
				if(permissionsBean.getPermissionsType()!=null && permissionsBean.getPermissionsType()==84){
					success=permissionsService.deletePermissions(permissionsBean.getPermissionsId());
					success=permissionsService.deletePermissions(permissionsId);
				}else if(permissionsBean.getPermissionsType()!=null && permissionsBean.getPermissionsType()==85){
					success=permissionsService.deletePermissions(permissionsBean.getPermissionsId());
					success=permissionsService.deletePermissions(permissionsId);
				}else{
					success=permissionsService.deletePermissions(permissionsId);
				}

			}
		} catch (Exception e) {
			logger.error("删除权限异常",e);
		}
		result.setIsSuccess(success);
		return result;
	}



	@Override
	public void queryPermissions(CommonPage commonPage) {
		permissionsService.queryPermissions(commonPage);

	}

	@Override
	public List<Permissions> queryPermissions(PermissionsExample example) {
		try {
			return permissionsService.queryPermissions(example);
		} catch (Exception e) {
			logger.error("查询权限异常",e);
		}
		return null;
	}

	public PermissionsBean queryPermissions(Integer permissionsId){
		try {
			return permissionsService.queryPermissions(permissionsId);
		} catch (Exception e) {
			logger.error("权限信息查询异常", e);
		}
		return null;
	}

	public PermissionsBean queryPermissionsByparentpermissionsId(Integer parentpermissionsId){
		try {
			return permissionsService.queryPermissionsByparentpermissionsId(parentpermissionsId);
		} catch (Exception e) {
			logger.error("权限信息查询异常", e);
		}
		return null;
	}





}
